package com.rhb.ca.role.dao;

import java.util.*;

import com.rhb.ca.role.bean.*;
import com.rhb.ca.util.UID;

import org.hibernate.Query;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class RoleDAO_HibernateImpl extends HibernateDaoSupport implements
		RoleDAO {
	static Logger logger = Logger.getLogger(RoleDAO_HibernateImpl.class);

	public void delete(Role obj) {
		try {
			this.getHibernateTemplate().delete(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Role findByPK(String pk) {
		return (Role) this.getHibernateTemplate().get(Role.class, pk);
	}

	public String create(Role obj) {
		String pk = UID.getUID();
		obj.setRolePk(pk);
		try {
			this.getHibernateTemplate().save(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pk;
	}

	public void update(Role obj) {
		try {
			this.getHibernateTemplate().update(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Query getQuery(String str,RoleQuery quester){
		StringBuffer hql = new StringBuffer(str);
		if (quester != null) {
											if (quester.getRoleName() != null && !"".equals(quester.getRoleName())) {
					hql.append(" and roleName like :roleName");
				}
								
															if (quester.getRoleCaption() != null && !"".equals(quester.getRoleCaption())) {
					hql.append(" and roleCaption like :roleCaption");
				}
								
									}

		Query query = this.getSession().createQuery(hql.toString());
		if (quester != null) {
											if (quester.getRoleName() != null && !"".equals(quester.getRoleName())) {
					query.setString("roleName","%"+String.valueOf(quester.getRoleName())+"%");
				}
								
															if (quester.getRoleCaption() != null && !"".equals(quester.getRoleCaption())) {
					query.setString("roleCaption","%"+String.valueOf(quester.getRoleCaption())+"%");
				}
								
									}
		return query;
	}

	public int getAllCount_findByQuery(RoleQuery quester) {
		Long allcount = new Long(0);
		String hql = "select count(role) from Role role where 1=1";
		try {
			Query query = getQuery(hql,quester);
			Iterator result = query.iterate();
			if (result.hasNext()) {
				allcount = (Long) result.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return allcount.intValue();
	}

	public List findByQuery(RoleQuery quester, int start, int count) {
		String hql = "select role from Role role where 1=1";
		try {
			Query query = getQuery(hql,quester);
			query.setFirstResult(start);
			query.setMaxResults(count);

			return query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Role findByRoleCaption(String no) {
		List list = null;
		String hql_find_by_no = "from Role where roleCaption=?";
		try {
			list = this.getHibernateTemplate().find(hql_find_by_no, no);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (list != null && list.isEmpty()) {
			return null;
		} else {
			Iterator it = list.iterator();
			Role role = (Role) it.next();
			return role;
		}
	}
	public boolean existRoleCaption(String no, String pk) {
		boolean flag = false;
		if (no == null || "".equals(no.trim())) {
			flag = false;
		} else {
			Long count = new Long(0);
			StringBuffer hql = new StringBuffer(
					"select count(role) from Role role where roleCaption='"
							+ no + "'");

			if (pk != null && !"".equals(pk.trim())) {
				hql.append(" and rolePk != '" + pk + "'");
			}

			try {
				Iterator result = this.getHibernateTemplate().find(
						hql.toString()).iterator();
				if (result.hasNext()) {
					count = (Long) result.next();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			if (count.equals(new Long(0))) {
				flag = false;
			} else {
				flag = true;
			}
		}
		return flag;
	}
}
